import $cache from '@/utils/cache.js';
import $api from '@/utils/request.js'
const user = {
	//检测用户是否登录
	isLogin() {
		if ($cache.get('token')) {
			return true
		}
		return false
	},
	//微信预注册登录
	async prelogin(params) {
		if (!$cache.get('token')) {
			let code = await this.getCode();
			console.log('code', code);
			const res = await $api('/login/mnpLogin', {
				code: code,
				referrer: params.referrer,
				avatar: params.avatar,
				nickname: params.nickname,
				iv: params.iv ?? '',
				encryptedData: params.encryptedData ?? ''
			})
			$cache.set('userInfo', res.data)
			$cache.set('token', res.data.token);
		}
	},
	//获取微信登录code
	getCode() {
		return new Promise((resolve, reject) => {
			uni.login({
				success(res) {
					if (res.errMsg === 'login:ok') {
						resolve(res.code)
					}
				}
			})
		})
	},
	//获取微信授权信息
	getUserInfo() {
		return new Promise((resolve, reject) => {
			uni.getUserInfo({
				provider: 'weixin',
				success(res) {
					console.log('getUserInfo', res)
					resolve(res)
				}
			})
		})
	}
}

export default user;